package edu.guet.cn.blogbasics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import edu.guet.cn.blogbasics.common.ResultEnum;
import edu.guet.cn.blogbasics.common.ResultVO;
import edu.guet.cn.blogbasics.entity.Users;
import edu.guet.cn.blogbasics.service.UserRoleService;
import edu.guet.cn.blogbasics.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hejianding
 * @since 2021-03-30
 */
@RestController
@Slf4j
public class UsersController {
    @Autowired
    UsersService usersService;
    @Autowired
    UserRoleService userRoleService;
    /**
     * 查看用户
     * @param userName
     * @return
     */
    @GetMapping("/users/getUsers")
    public Users getUsers( String userName){
        Users users = usersService.findByName(userName);
        return users;
    }
    /**
     * 注册
     * @param users
     * @return
     */
    @PostMapping("/common/registerUsers")
    public Map<String,Object> registerUsers(@RequestBody Users users){
        if (users.getUserName()==null||users.getPassword()==null||users.getNickName()==null){
            return ResultVO.result(ResultEnum.WRITE_MESSAGE,false);
        } else if(usersService.findByName(users.getUserName())!=null){
            return ResultVO.result(ResultEnum.USERNAME_EXIST,false);
        }else if (usersService.findByNickName(users.getNickName())!=null){
            return ResultVO.result(ResultEnum.NICKNAME_EXIST,false);
        }else {
            Users users1 = usersService.insertUsers(users);
            return ResultVO.result(ResultEnum.ADD_SUCCESSES,true);
        }
    }
    /**
     * 删除用户，需要管理员权限
     * @param userId
     */
    @PostMapping("/admin/deleteById")
    public void deleteUserById(int userId){
        usersService.removeById(userId);
        userRoleService.removeById(userId);
    }

    /**
     * 更改个人信息
     */
    @PostMapping("/users/update")
    public Map<String,Object> updateUserById(@RequestBody Users users){
        usersService.updateById(users);
        return ResultVO.result(ResultEnum.UPDATE_SUCCESS, true);
    }

    /**
     * 修改密码
     * @param userName
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/users/updatePassword")
    public Map<String,Object> updatePassWordByUserName(@RequestParam(value = "userName") String userName,
                                                       @RequestParam(value = "oldPassword") String oldPassword,
                                                       @RequestParam(value = "newPassword") String newPassword){
        System.out.println(userName +oldPassword+newPassword);
        QueryWrapper<Users> wrapper= Wrappers.query();
        wrapper.eq("userName",userName)
                .eq("password",oldPassword);
        Users users=usersService.getOne(wrapper);//4.29修改
        if (users!=null){
            usersService.updatePassword(userName,oldPassword,newPassword);
            return ResultVO.result(ResultEnum.UPDATE_PASSWORD_SUCCESS, true);
        }else {
            return ResultVO.result(ResultEnum.UPDATE_PASSWORD_FAILED, false);

        }
    }
}

